package ca.polymtl.liom.stim;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import ca.polymtl.liom.stim.LIOMStimulation;
import java.util.ArrayList;
import java.util.prefs.*;

public class LIOMStimulationList implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4869133846284063935L;
	private ArrayList<LIOMStimulation> p_stim_list = null;
	private String p_pref_key = "StimList";
	
	public LIOMStimulationList(){
		p_stim_list = new ArrayList<LIOMStimulation>();
	}
	/**
	 * This will add a new stimulation type to the list only if the name is unused.
	 * @param new_stim new stimulation type
	 * @return true if the stimulation was added, false otherwise
	 */
	public boolean addNewStimulation( LIOMStimulation new_stim ){
		boolean exist=false;
		for(int i=0; i<p_stim_list.size();i++){
			if(new_stim.name==p_stim_list.get(i).name){
				exist=true;
			}
		}
		if(!exist){
			p_stim_list.add(new_stim);
		}
		return exist;
	}
	
	/**
	 * Remove preferences associated with stimulation list.
	 */
	public void clearPreferences(){
		Preferences prefs = Preferences.userNodeForPackage(LIOMStimulationList.class);
		try{
			prefs.clear();
		}
		catch( BackingStoreException e){
			e.printStackTrace();
		}
	}
	
	public ArrayList<LIOMStimulation> getStimList(){
		return p_stim_list;
	}
	
	public void setToPreferences() throws IOException {	
		Preferences prefs = Preferences.userNodeForPackage(LIOMStimulationList.class);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream( baos );
		oos.writeObject( p_stim_list );
		prefs.putByteArray(p_pref_key, baos.toByteArray());
	}
	
	@SuppressWarnings("unchecked")
	public void getFromPreferences() throws IOException {
		Preferences prefs = Preferences.userNodeForPackage(LIOMStimulationList.class);
		try{
			byte[] raw=prefs.getByteArray(p_pref_key,null);
			ByteArrayInputStream bais = new ByteArrayInputStream( raw );
			ObjectInputStream ois = new ObjectInputStream( bais );
			p_stim_list = (ArrayList<LIOMStimulation>) ois.readObject();
		}
		catch( Exception e){
			// An exception here means that either there were not preferences or the backend store was 
			// not available. In either case, we want to create an empty stimulation list
			p_stim_list=new ArrayList<LIOMStimulation>();
		}
	}

	/**
	 * Main function for testing purposes
	 * @param args
	 */
	public static void main(String[] args) {
		LIOMStimulation a = new LIOMStimulation();
		a.name="Bloc";
		a.frequency=3;
		a.pulse_duration=0.3f;
		a.train_length=5;
		a.train_spacing=10;
		a.jitter=2;
		
		LIOMStimulation b = new LIOMStimulation();
		b.name="Event";
		b.frequency=3;
		b.pulse_duration=0.3f;
		b.train_length=1;
		b.train_spacing=10;
		b.jitter=2;
		
		LIOMStimulationList sl=new LIOMStimulationList();
		sl.addNewStimulation(a);
		sl.addNewStimulation(b);
		
		try{
			sl.setToPreferences();
			LIOMStimulationList sln=new LIOMStimulationList();
			//sln.clearPreferences();
			sln.getFromPreferences();
			ArrayList<LIOMStimulation> dd=sln.getStimList();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}

}
